<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleProduct"
          v-hasPermi="['order:topOrder:export']"
        >生产</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="topOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标识" align="center" prop="orderId" />
      <el-table-column label="订单号" align="center" prop="orderNumber" />
      <el-table-column label="工厂编号" align="center" prop="factoryNumber" />
      <el-table-column label="产线" align="center" prop="lineNumber" />
      <el-table-column label="车间编号" align="center" prop="workshopId" />
      <el-table-column label="车间名称" align="center" prop="workshopName" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.top_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleTesting(scope.row)"
            v-hasPermi="['order:topOrder:edit']"
          >生产</el-button>
          <el-button
            size="mini"
            type="button"
            icon="el-icon-edit"
            @click="handleTested(scope.row)"
            v-hasPermi="['order:topOrder:edit']"
          >结束</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:topOrder:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产计划订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">

        <el-form-item label="订单号" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入订单号" @keyup.enter.native="queryOrderSeqList"    @blur.native="queryOrderSeqList" />
        </el-form-item>
        <el-form-item label="车间标识" prop="workshopId">
          <el-input v-model="form.workshopId"  disabled />
        </el-form-item>
        <el-form-item label="车间名称" prop="workshopName">
          <el-input v-model="form.workshopName"  disabled />
        </el-form-item>
        
        <el-form-item label="选择产线">
              <el-select v-model="form.lineNumber" placeholder="请选择产线">
                <el-option v-for="item in lineList" :key="item.lineNo" :label="item.lineName" :value="item.lineNo"></el-option>
              </el-select>
        </el-form-item>
        
        <el-form-item label="选择订单工序">
              <el-select v-model="form.orderSeq" placeholder="请选择订单工序">
                <el-option v-for="item in orderSeqList" :key="item.seqName" :label="item.seqName" :value="item.seqName"></el-option>
              </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTopOrder, getTopOrder, delTopOrder, addTopOrder, updateTopOrder, testingOrder,completeOrder,getWorkshopList,getLineList, getOrderSeqList } from "@/api/order/topOrder";

export default {
  name: "TopOrder",
  dicts: ['top_order_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产计划订单表格数据
      topOrderList: [],
      // 工厂车间列表
      workshopList: [],
      // 工艺路线列表
      lineList: [],
      //订单工序列表
      orderSeqList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNumber: null,
        factoryNumber: null,
        orderType: null,
        description: null,
        materialNumber: null,
        totalQuantity: null,
        completedQuantity: null,
        orderUnit: null,
        beginDate: null,
        endDate: null,
        lineNumber: null,
        status: null,
        creator: null,
        updater: null,
      },
      // 表单参数
      form: {
        workshopId: "20000221",
        workshopName: "装置-整机调试车间",
        factoryNumber:"1030",
        orderNumber: null
      },
      // 表单校验
      rules: {
        orderNumber: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        workshopId: [
          { required: true, message: "车间标识不能为空", trigger: "blur" }
        ],
        workshopName: [
          { required: true, message: "车间名称不能为空", trigger: "blur" }
        ],
        workLine: [
          { required: true, message: "产线标识不能为空", trigger: "blur" }
        ],
        orderSeq: [
          { required: true, message: "订单工序不能为空", trigger: "blur" }
        ]

      }
    };
  },
  created() {
    this.getList();
    getLineList(this.form.workshopId).then(response => {
      this.lineList = response.rows;
    });
  },
  methods: {
    /** 查询生产计划订单列表 */
    getList() {
      this.loading = true;
      listTopOrder(this.queryParams).then(response => {
        this.topOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    queryOrderSeqList() {
      if (this.form.orderNumber) {
        getOrderSeqList(this.form.orderNumber).then(response => {
          this.orderSeqList = response.rows;
        });
      }
    },
    /** 生产操作 */
    handleTesting(row) {
      if(row.status !== '0') {
        this.$modal.msgWarning(`订单状态为"${row.status}"，非待生产状态，无法生产`)
        return
      }
      const ids = row.orderId
      this.$modal.confirm('是否确认对订单编号为"' + ids + '"进行生产？').then(function() {
        return testingOrder(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('提交生产成功')
      }).catch(() => {})
    },
    /** 完成生产操作 */
    handleTested(row) {
      if(row.status !== '1') {
        this.$modal.msgWarning(`订单状态为"${row.status}"，非生产中的状态，无法结束`)
        return
      }
      const ids = row.orderId
      this.$modal.confirm('是否确认对订单编号为"' + ids + '"进行生产结束？').then(function() {
        return completeOrder(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('提交结束订单成功')
      }).catch(() => {})
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        orderNumber: null,
        factoryNumber: null,
        orderType: null,
        description: null,
        materialNumber: null,
        totalQuantity: null,
        completedQuantity: null,
        orderUnit: null,
        beginDate: null,
        endDate: null,
        lineNumber: null,
        status: null,
        creator: null,
        createTime: null,
        updater: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 生产按钮操作 */
    handleProduct() {
      this.open = true;
      this.title = "生产订单确认";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getTopOrder(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产计划订单";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateTopOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTopOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除生产计划订单编号为"' + orderIds + '"的数据项？').then(function() {
        return delTopOrder(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('order/topOrder/export', {
        ...this.queryParams
      }, `topOrder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
